<template>
  <div>
    <vxe-toolbar id="toolbarId_bi5" :custom="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button v-if="!parentData.isDetail" @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      height="700"
      :data="tableData"
      :loading="loading"
      :sort-config="{ remote: true }"
      export-config
      show-overflow
      align="center"
      @sort-change="sortList"  
    >
    <vxe-table-column type="checkbox" width="60"  fixed="left" />
      <vxe-table-column>
        <vxe-table-column field="receiverUserName" min-width="130" title="实际收票人" sortable />
        <template v-slot:header>
          <el-select v-model="listQuery.receiverUserCode" placeholder="实际收票人" clearable>
            <el-option
              v-for="item in parentData.receiverUserCodeOptions"
              :key="item.code"
              :label="item.userName"
              :value="item.partyCode"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="invoiceTitle" min-width="130" title="发票抬头" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.invoiceTitle" placeholder="发票抬头" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="taxRegistrationNumber" min-width="130" title="纳税人识别号" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.taxRegistrationNumber" placeholder="纳税人识别号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="bankName" min-width="130" title="基本开户银行" sortable />
        <template v-slot:header>
          <el-select v-model="listQuery.bank" placeholder="基本开户银行" clearable>
            <el-option
              v-for="item in parentData.bankOptions"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="accountNumber" min-width="130" title="基本开户账号" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.accountNumber" placeholder="基本开户账号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="registeredAddress" min-width="130" title="注册场所地址" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.registeredAddress" placeholder="注册场所地址" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="registeredFixedPhone" min-width="130" title="注册固定电话" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.registeredFixedPhone" placeholder="注册固定电话" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="type"
          min-width="130"
          title="开票类型"
          :formatter="formatterType"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.type" placeholder="请选择开票类型" clearable>
            <el-option
              v-for="item in parentData.typeOptions"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="receiverPhone" min-width="130" title="关联电话" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.receiverPhone" placeholder="关联电话" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column v-if="!parentData.isDetail" min-width="100" title="操作"  field="action" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :page-sizes="Pagingconf"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
    <vxe-modal v-model="modelAddFlag" title="新增" width="800" height="500" resize>
      <compAdd
        :parent-data="parentData"
        @fatherAddMethod="fatherAdd"
        @fatherCancelMethod="fatherCancel"
      />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="800" height="500" resize>
      <compEdit
        ref="editRef"
        :parent-data="parentData"
        @fatherEditMethod="fatherEdit"
        @fatherCancelMethod="fatherCancel"
      />
    </vxe-modal>
  </div>
</template>
<script>
import compAdd from "./components/invoiceMessageAdd.vue";
import compEdit from "./components/invoiceMessageEdit.vue";
import { getInvoiceList, delInvoiceInfo } from "@/api/shipment";
export default {
  name: "InvoiceMessageList",
  components: {
    compAdd,
    compEdit
  },
  props: {
    parentData: {
      type: Object,
      default() {
        return {
          isDetail: false,
          areaOptions: [],
          goodsTypeList: [],
          typeOptions: [],
          vehicleDictList: [],
          bankOptions: [],
          receiverUserCodeOptions: []
        };
      }
    }
  },
  data() {
    return {
      loading: false,
      tableData: [],
      modelAddFlag: false,
      modelEditFlag: false,
      listQuery: {
        userCode: "",
        type: "",
        receiverUserCode: "",
        invoiceTitle: "",
        receiverPhone: "",
        taxRegistrationNumber: "",
        bank: "",
        accountNumber: "",
        registeredAddress: "",
        registeredFixedPhone: "",
        pageNum: 1,
        pageSize: 20,
        sortField: "",
        sortType: "",
        total: 0
      },
      Pagingconf: []
    };
  },
  created() {
    this.listQuery.userCode = this.$route.query.partyCode
      ? this.$route.query.partyCode
      : "";
    this.findList();
  },
  methods: {
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        getInvoiceList(this.listQuery).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.Pagingconf = [20, 30, 50, 100, response.data.total||1];
          this.loading = false;
          resolve();
        });
      });
    },
    formatterType({ cellValue }) {
      var item = this.parentData.typeOptions.find(
        item => item.code === cellValue.toString()
      );
      return item ? item.name : "";
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        var str;
        switch (property) {
          case "receiverUserName":
            str = "code";
            break;
          case "bankName":
            str = "bank";
            break;
          default:
            str = property;
            break;
        }
        this.listQuery.sortField = str;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },
    // 新增
    funcAdd() {
      this.modelAddFlag = true;
    },
    // 编辑
    funcEdit(code) {
      // 调用子组件方法
      this.$refs.editRef.getInfo(code);
      this.modelEditFlag = true;
    },
    // 删除方法
    funcDelete(codes) {
      this.$confirm("确认删除吗？")
        .then(_ => {
          delInvoiceInfo({ codes }).then(response => {
            this.$message({ message: response.msg, type: "success" });
            this.findList();
          });
        })
        .catch(_ => {});
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false;
      this.findList();
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false;
      this.findList();
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case "add":
          this.modelAddFlag = false;
          break;
        case "edit":
          this.modelEditFlag = false;
          break;
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    }
  }
};
</script>
